//
//  MemberViewController.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/20.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "MemberViewController.h"
#import "LogonViewController.h"
#import <Masonry/Masonry.h>
#import "ResponseManager.h"
#import "ResponseManager+LogonInfo.h"
#import "UserModel.h"
#import "FBProgressView.h"
#import "UserState.h"

@interface MemberViewController (){
    UITextField *userName;
    UITextField *passWord;
    UIButton *loginButton;
    
}

@end

@implementation MemberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLOR(241.0f, 241.0f, 241.0f, 1);
    [self setupBackItem];
    [self setupRightItemWithText:@"注册" action:@selector(logonButtonTaped:)];
    [self loadData];
    [self pasword];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(pasword) name:UITextFieldTextDidChangeNotification object:nil];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)logonButtonTaped:(UIButton *)sender {
    
    LogonViewController *logonVC = [[LogonViewController alloc]init];
    [self.navigationController pushViewController:logonVC animated:YES];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}



- (void)loadData {
    
    userName = [self memberTextFieldText:@"请输入手机号" image:@"登录注册-手机" label:nil];
    userName.text = @"13118108475";
    [self.view addSubview:userName];
    
    [userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(74);
        make.height.mas_equalTo(50);
    }];
    
   passWord = [self memberTextFieldText:@"请输入密码" image:@"登录注册-钥匙" label:nil];
    passWord.text = @"a2825001";
    passWord.secureTextEntry = YES;
    [self.view addSubview:passWord];

    [passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(userName.mas_bottom).with.offset(1);
        make.height.mas_equalTo(50);
    }];
    
    
    
    loginButton = [[UIButton alloc]init];
    loginButton.backgroundColor = RGBCOLOR(170.0f, 170.0f, 170.0f, 1);
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton setTitleColor:RGBCOLOR(205.0f, 205.0f, 205.0f, 1) forState:UIControlStateHighlighted];
    loginButton.titleLabel.textColor = [UIColor whiteColor];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:15];
    loginButton.layer.cornerRadius = 5;
    [self.view addSubview:loginButton];
    
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(passWord.mas_bottom).with.offset(30);
        make.height.mas_equalTo(50);
        
    }];
    
}

- (void)pasword {
    if (passWord.text.length == 0 || userName.text.length == 0) {
        loginButton.backgroundColor = RGBCOLOR(170.0f, 170.0f, 170.0f, 1);
    }
    if (passWord.text.length >= 1 && userName.text.length >= 1) {
        loginButton.backgroundColor = BLUE_COLOR;
        [loginButton addTarget:self action:@selector(loginButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)loginButtonTaped:(UIButton *)button {
    __weak typeof(self) myself = self;
    [FBProgressView showWithMessage:nil];
    [ResponseManager getUserInfoLoginUserName:userName.text passWord:passWord.text completionHandler:^(FBResponse *response) {
        if (response.success) {
            [FBProgressView hide];
            [[EMClient sharedClient]asyncLoginWithUsername:userName.text password:passWord.text success:^{
                UserModel *model = [UserModel sharedUserModel];
                model.islogin = YES;
//                UserState *user = [[UserState alloc]init];
//                user.index = 0;
                NSLog(@"%@",response.resultValue);
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                NSDictionary *dic = @{@"username":userName.text,@"password":passWord.text,@"id":response.resultValue[@"id"]};
                [userDefaults setObject:dic forKey:@"userInfo"];
                [userDefaults synchronize];
                [[NSNotificationCenter defaultCenter]postNotificationName:@"loginStateChanged" object:@(YES)];
               [myself.navigationController popViewControllerAnimated:YES];
            } failure:^(EMError *aError) {
                [myself addAlertControllerWithTitle:@"提示" message:aError.errorDescription actionNum:2 style:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
                    [myself loginButtonTaped:loginButton];
                    }];
                }];
            }else {
            [FBProgressView hide];
            [self addAlertControllerWithTitle:@"提示" message:response.resultDesc actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
        }
        
    }];
    
}








@end
